package com.xvyy.tingshu.album.service.impl;

import com.xvyy.tingshu.album.mapper.*;
import com.xvyy.tingshu.album.service.BaseCategoryService;
import com.xvyy.tingshu.common.execption.TsException;
import com.xvyy.tingshu.model.album.*;
import com.xvyy.tingshu.vo.album.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    /**
     * 优化
     *
     * @return
     */
    @Override
    public List<CategoryVo> getBaseCategoryList() {
        return baseCategoryViewMapper.getBaseCategoryList();
    }

    @Override
    public List<BaseAttribute> findAttributeAndValue(Long category1Id) {
        return baseAttributeMapper.findAttributeAndValue(category1Id);
    }

    @Override
    public BaseCategoryView getAlbumInfoCategory(Long albumId) {
        return baseCategoryViewMapper.getAlbumInfoCategory(albumId);
    }


    @Override
    public List<BaseCategory3> getC1Id7TopBaseCategory3List(Long c1Id) {

        // 1. 根据c1Id查询二级分类对象
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory2>()
                        .eq(BaseCategory2::getCategory1Id, c1Id)// 只查询对应一级分类的二级分类对象
                        .eq(BaseCategory2::getIsDeleted, 0));// 删除标记为0的记录
        if (CollectionUtils.isEmpty(baseCategory2List)) {
            throw new TsException(201, "该一级分类下没有二级分类信息");
        }
        // 2. 根据二级分类id查询三级分类对象
        List<Long> c2Ids = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory3>()// 查询条件构造器
                        .in(BaseCategory3::getCategory2Id, c2Ids)// 只查询对应二级分类的三级分类对象
                        .eq(BaseCategory3::getIsTop, 1)// 只查询置顶的三级分类对象
                        .last("limit 7")// 限制查询7条数据
                        .eq(BaseCategory3::getIsDeleted, 0));// 删除标记为0的记录
        if (CollectionUtils.isEmpty(baseCategory3List)) {
            return new ArrayList<>();
        }
        return baseCategory3List;
    }

    /**
     * getBaseCategoryList 初始版
     * @param c1Id
     * @return
     */
/*    @Override
    public CategoryVo getBaseCategoryList(Long c1Id) {

        //优化： 递归 或者写SQL语句

        CategoryVo categoryVo = new CategoryVo();

        // 1. 根据c1Id查询一级分类对象
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(c1Id);
        if (baseCategory1 == null) {
            throw new TsException(201, "一级分类不存在");
        }
        // 2. 根据c1Id查询二级分类对象
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory2>()
                        .eq(BaseCategory2::getCategory1Id, c1Id));
        if (CollectionUtils.isEmpty(baseCategory2List)) {
            throw new TsException(201, "一级分类下没有二级分类对象");
        }
        List<CategoryVo> category1List = new ArrayList<>();
        for (BaseCategory2 baseCategory2 : baseCategory2List) {
            CategoryVo category2Vo = new CategoryVo();
            category2Vo.setCategoryId(baseCategory2.getId());// 二级分类的id
            category2Vo.setCategoryName(baseCategory2.getName());// 二级分类的名称
            // 3. 根据二级分类id查询三级分类对象
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(
                    new LambdaQueryWrapper<BaseCategory3>()
                            .eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
            if (CollectionUtils.isEmpty(baseCategory3List)) {
                throw new TsException(201, "二级分类下没有三级分类对象");

            }
            List<CategoryVo> category2List = new ArrayList<>();
            for (BaseCategory3 baseCategory3 : baseCategory3List) {
                CategoryVo category3Vo = new CategoryVo();
                category3Vo.setCategoryId(baseCategory3.getId());// 三级分类的id
                category3Vo.setCategoryName(baseCategory3.getName());// 三级分类的名称
                category3Vo.setCategoryChild(Lists.newArrayList());// 三级分类的孩子
                category2List.add(category3Vo);
            }
            category2Vo.setCategoryChild(category2List);
            category1List.add(category2Vo);
        }
        categoryVo.setCategoryId(c1Id);// 一级分类的id
        categoryVo.setCategoryName(baseCategory1.getName());// 一级分类的名称
        categoryVo.setCategoryChild(category1List);

        // 4.返回数据
        return categoryVo;
    }*/

    /**
     * getBaseCategoryList 递归优化
     * @param c1Id
     * @return categoryVo 分类树结构对象
     */
    @Override
    public CategoryVo getBaseCategoryList(Long c1Id) {
        // 根据c1Id查询一级分类对象
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(c1Id);
        if (baseCategory1 == null) {
            throw new TsException(201, "一级分类不存在");
        }

        // 递归加载分类信息
        CategoryVo categoryVo = buildCategoryTree(c1Id);
        categoryVo.setCategoryId(c1Id); // 一级分类的id
        categoryVo.setCategoryName(baseCategory1.getName()); // 一级分类的名称

        return categoryVo;
    }

    @Override
    public List<Long> getCategory1IdList() {

        List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);
        return baseCategory1List.stream().map(BaseCategory1::getId).collect(Collectors.toList());

    }

    private CategoryVo buildCategoryTree(Long parentId) {
        CategoryVo categoryVo = new CategoryVo();

        // 查询二级分类
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory2>()
                        .eq(BaseCategory2::getCategory1Id, parentId));

        if (CollectionUtils.isEmpty(baseCategory2List)) {
            throw new TsException(201, "一级分类下没有二级分类对象");
        }

        List<CategoryVo> category1List = new ArrayList<>();
        for (BaseCategory2 baseCategory2 : baseCategory2List) {
            CategoryVo category2Vo = new CategoryVo();
            category2Vo.setCategoryId(baseCategory2.getId()); // 二级分类的id
            category2Vo.setCategoryName(baseCategory2.getName()); // 二级分类的名称

            // 查询三级分类并递归组装
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(
                    new LambdaQueryWrapper<BaseCategory3>()
                            .eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));

            if (CollectionUtils.isEmpty(baseCategory3List)) {
                throw new TsException(201, "二级分类下没有三级分类对象");
            }

            List<CategoryVo> category2List = baseCategory3List.stream().map(baseCategory3 -> {
                CategoryVo category3Vo = new CategoryVo();
                category3Vo.setCategoryId(baseCategory3.getId()); // 三级分类的id
                category3Vo.setCategoryName(baseCategory3.getName()); // 三级分类的名称
                category3Vo.setCategoryChild(Collections.emptyList()); // 三级分类没有子分类
                return category3Vo;
            }).collect(Collectors.toList());

            category2Vo.setCategoryChild(category2List);
            category1List.add(category2Vo);
        }

        categoryVo.setCategoryChild(category1List);
        return categoryVo;
    }


    /**
     * 未优化
     *
     * @return
     */
/*    @Override
    public List<CategoryVo> getBaseCategoryList() {
		// 0. 定义一个返回结果集合对象
        List<CategoryVo> categoryVoArrayList = new ArrayList<>();
		// 1. 查询所有的分类信息
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        // 2.封装数据
		// category1IdAndChildrenMap: key:所有的一级分类id   一级各自一级分类的孩子
        Map<Long, List<BaseCategoryView>> category1IdAndChildrenMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdAndChrildrenEntry : category1IdAndChildrenMap.entrySet()) {
			// 一级分类对象的创建
            CategoryVo category1Vo = new CategoryVo();
            category1Vo.setCategoryId(category1IdAndChrildrenEntry.getKey());// 一级分类的id
            List<BaseCategoryView> category1ChildrenList = category1IdAndChrildrenEntry.getValue();
            category1Vo.setCategoryName(category1ChildrenList.get(0).getCategory1Name());// 一级分类的名称
			ArrayList<CategoryVo> category1List = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> category2IdAndChrildrenMap = category1ChildrenList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdAndChrildrenEntry : category2IdAndChrildrenMap.entrySet()) {
				// 二级分类对象的创建
                CategoryVo category2Vo = new CategoryVo();
                category2Vo.setCategoryId(category2IdAndChrildrenEntry.getKey());// 二级分类的id
                List<BaseCategoryView> category2childrenList = category2IdAndChrildrenEntry.getValue();
                category2Vo.setCategoryName(category2childrenList.get(0).getCategory2Name());// 二级分类的名称
				ArrayList<CategoryVo> category2List = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : category2childrenList) {
					// 三级分类对象的创建
                    CategoryVo category3Vo = new CategoryVo();
                    category3Vo.setCategoryId(baseCategoryView.getCategory3Id());// 三级分类的id
                    category3Vo.setCategoryName(baseCategoryView.getCategory3Name());// 三级分类的名称
                    category3Vo.setCategoryChild(new ArrayList<>());
					category2List.add(category3Vo);
                }
                category2Vo.setCategoryChild(category2List); // 二级分类孩子
				category1List.add(category2Vo);
            }
            category1Vo.setCategoryChild(category1List); // 一级分类孩子
			categoryVoArrayList.add(category1Vo);// 将遍历得到的每一个一级分类对象添加到集合中
        }
        return categoryVoArrayList;
    }*/


}
